package com.cloudkinto.controller.stock;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.stock.StockLogService;
import com.cloudkinto.service.stock.vo.log.StockLogPageRes;
import io.swagger.annotations.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 张永远
 * @since 2020-06-10
 */
@RestController
@Api(tags = "库存日志模块")
@RequestMapping("/api/stockLog")
public class StockLogController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(StockLogController.class);

    @Autowired
    private StockLogService service;

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    @ApiResponses({@ApiResponse(code = 200, message = "", response = StockLogPageRes.class)})
    public SingleResult pageInit() {

        return service.pageInit(getCompanyId(), getCurrentUserId());
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "shopId", value = "店铺id"),
            @ApiImplicitParam(name = "storageId", value = "仓库id"),
            @ApiImplicitParam(name = "keyword", value = "关键字（模糊查询）"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "type", value = "操作类型"),
            @ApiImplicitParam(name = "status", value = "产品类型"),
            @ApiImplicitParam(name = "startTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间")})
    @ApiResponses({@ApiResponse(code = 200, message = "", response = StockLogPageRes.class)})
    public PageResult<StockLogPageRes> listPage(HttpServletRequest request) {
        Long companyId = getCompanyId();
        Map<String, String> map = new HashMap<>();
        String keyword = request.getParameter("keyword");
        String shopId = request.getParameter("shopId");
        String startDate = request.getParameter("startTime");
        String endDate = request.getParameter("endTime");
        String storageId = request.getParameter("storageId");
        String type = request.getParameter("type");
        String orderType = request.getParameter("orderType");
        String status = request.getParameter("status");
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");

        map.put("keyword", keyword);
        if (StringUtils.isNotBlank(shopId)) {
            map.put("shopId", shopId);
        } else if (getShopId() != null) {
            map.put("shopId", getShopId() + "");
        }
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        map.put("storageId", storageId);
        map.put("status", status);
        map.put("type", type);
        map.put("orderType", orderType);
        map.put("companyId", companyId + "");
        map.put("current", current);
        map.put("pageSize", size);
        return service.getListPage(map);
    }

    @UserSessionRequired
    @GetMapping("/exportExcel")
    @ApiOperation(value = "库存日志导出Excel")
    @ApiImplicitParams({@ApiImplicitParam(name = "shopId", value = "店铺id"),
            @ApiImplicitParam(name = "storageId", value = "仓库id"),
            @ApiImplicitParam(name = "keyword", value = "关键字（模糊查询）"),
            @ApiImplicitParam(name = "type", value = "操作类型"),
            @ApiImplicitParam(name = "status", value = "产品类型"),
            @ApiImplicitParam(name = "startTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间")})
    public SingleResult exportExcel(String shopId, String storageId, String keyword,String type,String orderType,String startTime, String endTime) throws IOException {
        Map<String, String> map = new HashMap<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String time = simpleDateFormat.format(new Date());
        String fileName = "库存日志" + time +".xls";
        map.put("keyword", keyword);
        map.put("type", type);
        map.put("startDate", startTime);
        map.put("endDate", endTime);
        map.put("shopId", shopId);
        map.put("orderType", orderType);
        map.put("storageId", storageId);
        map.put("companyId", getCompanyId() + "");
        return SingleResult.success(service.exportExcel(map,fileName));
    }
}

